package com.hnjfzn.mall.service;

import com.hnjfzn.mall.domain.UserAddress;
import com.hnjfzn.mall.mapper.UserAddressMapper;
import com.hnjfzn.mall.utils.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    public boolean addUserAddress(UserAddress userAddress) {
        // 生成唯一的 addressId
        long addressId = snowflakeIdGenerator.generateId();

        // 设置创建时间和更新时间
        Date currentTime = new Date();
        userAddress.setCreateTime(currentTime);
        userAddress.setUpdateTime(currentTime);
        userAddress.setAddressId(addressId);  // 设置生成的唯一 ID

        // 插入到数据库
        return userAddressMapper.insert(userAddress) > 0;
    }


    public List<UserAddress> getUserAddressesByUserId(Long userId) {
        return userAddressMapper.selectList(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<UserAddress>()
                        .eq("user_id", userId)
        );
    }

    public UserAddress getUserAddress(Long addressId) {
        return userAddressMapper.selectById(addressId);
    }

    public boolean updateUserAddress(UserAddress userAddress) {
        return userAddressMapper.updateById(userAddress) > 0;
    }

    public boolean deleteUserAddress(Long addressId) {
        return userAddressMapper.deleteById(addressId) > 0;
    }
}